home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / WINSRC20.ZIP / GIFVIEW.C < prev    next >
Text File  |  1990-09-09  |  6KB  |  231 lines

  1. /*
  2.  *
  3.  * This GIF decoder is designed for use with Bert Tyler's FRACTINT
  4.  * program. It should be noted that the "FRACTINT" program only decodes
  5.  * GIF files FRACTINT creates, so this decoder code lacks full generality
  6.  * in the following respects: supports single image, non-interlaced GIF
  7.  * files with no local color maps and no extension blocks.
  8.  *
  9.  * GIF and 'Graphics Interchange Format' are trademarks (tm) of
  10.  * Compuserve, Incorporated, an H&R Block Company.
  11.  *
  12.  *                                            Tim Wegner
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <dos.h>
  18. #include "fractint.h"
  19.  
  20. /* routines in this module    */
  21.  
  22. int  gifview(void);
  23. int  get_byte(void);        /* used only locally and by decoder.c */
  24.  
  25. static void close_file(void);
  26.  
  27. #define MAXCOLORS    256
  28.  
  29. extern int timer(int timertype,int(*subrtn)(),...);
  30. extern int rowcount;        /* row counter for screen */
  31. extern char readname[];     /* file name          */
  32. static FILE *fpin = NULL;    /* FILE pointer       */
  33. unsigned int height;
  34.  
  35. extern    char MAP_name[];
  36. extern    int    mapset;
  37.  
  38. extern int glassestype;
  39. extern int display3d;
  40. extern int dotmode;        /* so we can detect disk-video */
  41. extern int calc_status;
  42. extern long calctime;
  43. extern long timer_interval;
  44. extern int pot16bit;        /* 16 bit values for continuous potential */
  45.  
  46. int bad_code_count = 0;     /* needed by decoder module */
  47.  
  48. int get_byte()
  49. {
  50.    return (getc(fpin)); /* EOF is -1, as desired */
  51. }
  52.  
  53. extern unsigned char dacbox[256][3];    /* Video-DAC (filled in by SETVIDEO) */
  54. extern int reallyega;            /* "really-an-ega" flag */
  55. extern int paletteVGA[16];        /* VGA Palette-to-DAC registers */
  56. extern unsigned char decoderline[2049]; /* write-line routines use this */
  57.  
  58. /* Main entry decoder */
  59. int gifview()
  60. {
  61.    unsigned numcolors;
  62.    unsigned char buffer[16];
  63.    unsigned width, finished;
  64.    char temp1[81];
  65.  
  66.    int status;
  67.    int i, j, k, planes;
  68.  
  69.    status = 0;
  70.  
  71.    /* initialize the row count for write-lines */
  72.    rowcount = 0;
  73.  
  74.    /* zero out the full write-line */
  75.    for (width = 0; width < 2049; width++) decoderline[width] = 0;
  76.  
  77.    /* Open the file */
  78.    strcpy(temp1,readname);
  79.    if (strchr(temp1,'.') == NULL) {
  80.       strcat(temp1,DEFAULTFRACTALTYPE);
  81.       if ((fpin = fopen(temp1,"rb")) != NULL) {
  82.      fclose(fpin);
  83.      }
  84.       else {
  85.      strcpy(temp1,readname);
  86.      strcat(temp1,ALTERNATEFRACTALTYPE);
  87.      }
  88.       }
  89.    if ((fpin = fopen(temp1, "rb")) == NULL)
  90.       return (-1);
  91.  
  92.    /* Get the screen description */
  93.    for (i = 0; i < 13; i++)
  94.    {
  95.       if ((buffer[i] = (unsigned char)get_byte ()) < 0)
  96.       {
  97.      close_file();
  98.      return(-1);
  99.       }
  100.    }
  101.  
  102.    if(strncmp(buffer,"GIF87a",3) ||             /* use updated GIF specs */
  103.       buffer[3] < '0' || buffer[3] > '9' ||
  104.       buffer[4] < '0' || buffer[4] > '9' ||
  105.       buffer[5] < 'A' || buffer[5] > 'z' )
  106.    {
  107.       close_file();
  108.       return(-1);
  109.    }
  110.  
  111.    planes = (buffer[10] & 0x0F) + 1;
  112.  
  113.    if((buffer[10] & 0x80)==0)     /* color map (better be!) */
  114.    {
  115.       close_file();
  116.       return(-1);
  117.    }
  118.    numcolors = 1 << planes;
  119.  
  120.    for (i = 0; i < numcolors; i++)
  121.    {
  122.       if (numcolors == 16 && !reallyega)    /* straight copy or indirect via palette? */
  123.      k = paletteVGA[i];
  124.       else
  125.      k = i;
  126.       for (j = 0; j < 3; j++) {
  127.      if ((buffer[j] = (unsigned char)get_byte()) < 0)
  128.      {
  129.         close_file();
  130.         return(-1);
  131.      }
  132.      if (dacbox[0][0] != 255)    /* (only if we really have a DAC) */
  133.         if(!display3d || (glassestype != 1 && glassestype != 2))
  134.            dacbox[k][j] = buffer[j] >> 2;
  135.       }
  136.    }
  137.    /* don't read if glasses */
  138.    if (display3d && mapset && glassestype!=1 && glassestype != 2)
  139.    {
  140.        ValidateLuts(MAP_name);    /* read the palette file */
  141.        if (dotmode != 11)
  142.       spindac(0,1); /* load it, but don't spin */
  143.    }
  144.    if (dacbox[0][0] != 255 && dotmode != 11)
  145.       spindac(0,1);      /* update the DAC */
  146.  
  147.    if (dotmode == 11) /* disk-video */
  148.    {
  149.        movecursor(2,0);
  150.        printf("...restoring...");
  151.    }
  152.  
  153.    /* Now display one or more GIF objects */
  154.    finished = 0;
  155.    while (!finished)
  156.    {
  157.       switch (get_byte())
  158.       {
  159.       case ';':
  160.      /* End of the GIF dataset */
  161.  
  162.      finished = 1;
  163.      status = 0;
  164.      break;
  165.  
  166.       case '!':                               /* GIF Extension Block */
  167.      get_byte();             /* read (and ignore) the ID */
  168.      while ((i = get_byte()) > 0)     /* get the data length */
  169.         for (j = 0; j < i; j++)
  170.            get_byte();     /* flush the data */
  171.      break;
  172.       case ',':
  173.      /*
  174.       * Start of an image object. Read the image description.
  175.       */
  176.  
  177.      for (i = 0; i < 9; i++)
  178.      {
  179.         if ((buffer[i] = (unsigned char)get_byte ()) < 0)
  180.         {
  181.            status = -1;
  182.            break;
  183.         }
  184.      }
  185.      if(status < 0)
  186.      {
  187.         finished = 1;
  188.         break;
  189.      }
  190.  
  191.      width    = buffer[4] | buffer[5] << 8;
  192.      if (pot16bit) width >>= 1;
  193.      height = buffer[6] | buffer[7] << 8;
  194.  
  195.      /* Setup the color palette for the image */
  196.  
  197.      if (calc_status == 1) /* should never be so, but make sure */
  198.         calc_status = 0;
  199.      status = timer(1,NULL,width); /* call decoder(width) via timer */
  200.      if (calc_status == 1) /* e.g., set by line3d */
  201.      {
  202.         calctime = timer_interval; /* note how long it took */
  203.         if (keypressed() != 0)
  204.            calc_status = 3; /* interrupted, not resumable */
  205.         else
  206.            calc_status = 4; /* complete */
  207.      }
  208.      finished = 1;
  209.      break;
  210.       default:
  211.      status = -1;
  212.      finished = 1;
  213.      break;
  214.       }
  215.    }
  216.    close_file();
  217.    if (dotmode == 11) /* disk-video */
  218.    {
  219.       home();
  220.       printf("Restore completed        \n\n                    ");
  221.    }
  222.  
  223.    return(status);
  224. }
  225.  
  226. static void close_file()
  227. {
  228.    fclose(fpin);
  229.    fpin = NULL;
  230. }
  231.